package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.google.gson.Gson;
import com.model.Branch;
import com.model.Branch;
import com.service.BranchService;

@Controller
public class BranchController {
	@Autowired
	private BranchService branchService;
	
	
	// REquest to add branch.. Give blank form of branch
	@RequestMapping(value="/admin/addBranch",method=RequestMethod.GET)
	public ModelAndView addBranch(@ModelAttribute("command") Branch branch) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("branchList",branchService.getAll());
		return new ModelAndView("branch",model);
	}
	
	// add branch details into database
	@RequestMapping(value = "/admin/saveBranch", method = RequestMethod.POST)
	public String saveBranch(@ModelAttribute("command") Branch branch) {
			branchService.add(branch);
			return "redirect:/admin/addBranch.htm";
	}
	
	// give branch details from database
	@RequestMapping("/admin/editBranch")
    public ModelAndView editBranch(@ModelAttribute("command") Branch branch,
    		HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("branch",branchService.get(id));
        return new ModelAndView("branch",model);
    }
	
	//delete Branch
	@RequestMapping("/admin/deleteBranch")
    public String deleteBranch(HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
        branchService.delete(id);
        return "redirect:/admin/addBranch.htm";
    }



}
